<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Http\Requests\RoleRequest;
use Spatie\Permission\Models\Role;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	$roles=Role::where('name','<>','webmaster')->get();
        return view('admin::role.index',compact ('roles'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(RoleRequest $request)
    {
		Role::create($request->all());
		flash('角色添加成功！')->overlay ();
		return redirect ()->back();
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(RoleRequest $request,$id)
    {
    	$role=Role::find($id);
    	$role->name=$request->input('name');
    	$role->title=$request->input('title');
    	$role->save();
    	flash('编辑成功！')->overlay ();
    	return redirect ()->back();
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy()
    {
    }
    public function roledel($id){
		Role::destroy ($id);
	}
	public function showpermission($id){
		$role=Role::find($id);
		$permissions = \HDModule::getPermissionByGuard('admin');
		//dd($permissions);
    	return view('admin::role.showpermission',compact ('role','permissions'));
	}
	public function addpermission(Request $request,$id){
		if (!$request->input('name')){
			flash('最少选择一个权限')->overlay ();
			return back();
		}
    	$permissions=$request->input('name');
    	$role=Role::find($id);
		$role->syncPermissions($permissions);
		flash('分配权限成功！')->overlay ();
		return redirect ('/admin/role');
	}
}
